package com.innovation.config;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.dom4j.Document;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;

public class GlobalConfig {
	
	private static final String CONFIG_FILE_PATH = "config.xml";//配置文件类路径
	public static String KEY = null;
	public static ZoneMode MODE = ZoneMode.RELEASE ;//模式
	public static DBConfig RESDB = null;
	
	/**
	 * 区列表
	 */
	private static Map<Integer, ZoneConfig> zoneConfigMap = new HashMap<Integer, ZoneConfig>();
	
	/**
	 * 战斗Server列表
	 */
	private static Map<Integer, ServerConfig> fightServerConfigMap = new HashMap<Integer, GlobalConfig.ServerConfig>();
	
	public static class ZoneConfig{
		public int zoneId;//区id
		public String host;//主机ip
		public int websocketPort;//websocket端口
		public int socketPort;//socket端口
		public int adminPort;//管理员端口
		
		public int maxOnline = 2000;//最大在线人数
		
		public Map<Integer, DBConfig> dbMap = new HashMap<Integer, DBConfig>();//区DB列表
		public MongoConfig mongoConfig = null;
	}
	
	public static class ServerConfig{
		public int id;
		public String host;//主机ip
		public int socketPort;//socket端口
	}
	
	public static class DBConfig{
		public int id;
		public String userName;
		public String password;
		public String url;
		public String driver;
	}
	
	public static class MongoConfig{
		public String host;
		public int port;
		public String dbName;
	}
	
	public static enum ZoneMode {
		TEST,
		RELEASE
	}
	
	/**
	 * 初始化配置信息
	 * @throws Exception
	 */
	@SuppressWarnings("unchecked")
	public static void init() throws Exception {
		SAXReader reader = new SAXReader();
		Document document = reader.read(GlobalConfig.class.getClassLoader().getResourceAsStream(CONFIG_FILE_PATH));
		Element root = document.getRootElement();
		if (root == null) {
			throw new Exception("no root node in config");
		}
		// 全局变量
		Element global = root.element("global");
		if (global == null) {
			throw new Exception("no global node in config");
		}
		
		String mode = global.attributeValue("mode");
		if(mode != null && mode.equals("test")){
			MODE = ZoneMode.TEST;
		}
		KEY = global.attributeValue("key");
		
		//资源库
		Element resElement = global.element("res");
		if (resElement == null) {
			throw new Exception("no res node in config");
		}
		RESDB = createDBConfig(resElement);
		
		//zone列表
		List<Element> zones = root.elements("zone");
		for(Element zoneElement : zones){
			ZoneConfig zone = new ZoneConfig();
			zone.zoneId = Integer.valueOf(zoneElement.attributeValue("id"));
			zone.host = zoneElement.attributeValue("host");
			zone.websocketPort = Integer.valueOf(zoneElement.attributeValue("websocketPort"));
			zone.socketPort = Integer.valueOf(zoneElement.attributeValue("socketPort"));
			zone.adminPort = Integer.parseInt(zoneElement.attributeValue("adminPort"));
			
			Element dbElement = zoneElement.element("db");
			if (dbElement == null) {
				throw new Exception("no db element in zone = " + zone.zoneId);
			}
			List<Element> dbs = dbElement.elements("server");
			for(Element db : dbs){
				DBConfig dbConfig = createDBConfig(db);
				zone.dbMap.put(dbConfig.id, dbConfig);
			}
			
			//mongodb配置
			Element mongoElement = dbElement.element("master");
			MongoConfig mongoConfig = new MongoConfig();
			mongoConfig.host = mongoElement.attributeValue("host");
			mongoConfig.port = Integer.valueOf(mongoElement.attributeValue("port"));
			mongoConfig.dbName = mongoElement.attributeValue("dbname");
			zone.mongoConfig = mongoConfig;
			
			zoneConfigMap.put(zone.zoneId, zone);
		}
		
		//FightServer
		Element fightElement = root.element("FightServer");
		List<Element> serverElements = fightElement.elements("server");
		for(Element serverElement : serverElements){
			ServerConfig config = new ServerConfig();
			config.host = serverElement.attributeValue("host");
			config.socketPort = Integer.valueOf(serverElement.attributeValue("socketPort"));
			config.id = Integer.valueOf(serverElement.attributeValue("id"));
			
			fightServerConfigMap.put(config.id, config);
		}
	}
	
	private static DBConfig createDBConfig(Element dbElement){
		DBConfig config = new DBConfig();
		config.id = Integer.valueOf(dbElement.attributeValue("id"));
		config.userName = dbElement.attributeValue("username");
		config.password = dbElement.attributeValue("password");
		config.url = dbElement.attributeValue("url");
		config.driver = dbElement.attributeValue("driver");
		return config;
	}
	
	public static ZoneConfig getZoneConfig(int zoneId){
		return zoneConfigMap.get(zoneId);
	}
	
	/**
	 * 返回所有区的配置
	 * @return
	 */
	public static Map<Integer, ZoneConfig> getZoneConfigMap(){
		return zoneConfigMap;
	}
	
	/**
	 * 返回所有战斗Server配置信息
	 * @return
	 */
	public static Map<Integer, ServerConfig> getFightServerConfigMap(){
		return fightServerConfigMap;
	}
	
	public static void main(String[] args) {
		try {
			init();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}
